package com.kaka.springboot.order.rabbitmq.producer.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrder(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);

        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        //@param1 交换机 @param2 路由key/queue队列名称 @param3 消息内容
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }

    public void makeOrderDirect(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);

        String exchangeName = "direct_order_exchange";
//        String routingKey = "";
        //@param1 交换机 @param2 路由key/queue队列名称 @param3 消息内容
        rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);
    }

    public void makeOrderTopic(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);

        String exchangeName = "topic_order_exchange";
        String routingKey = "com.duanxin";
        //@param1 交换机 @param2 路由key/queue队列名称 @param3 消息内容
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }

    //给队列设置过期时间
    public void makeOrderTTL(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);

        String exchangeName = "ttl_order_exchange";
        String routingKey = "ttl";
        //@param1 交换机 @param2 路由key/queue队列名称 @param3 消息内容
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }

    public void makeOrderMessageTTL(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);

        String exchangeName = "ttl_order_exchange";
        String routingKey = "ttlmessage";

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };

        //@param1 交换机 @param2 路由key/queue队列名称 @param3 消息内容
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId,messagePostProcessor);
    }
}